package nl.arjanfrans.stopwatch.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import nl.arjanfrans.stopwatch.R;
import nl.arjanfrans.stopwatch.main.Stopwatch;

public class LapsFragments extends Fragment {
    private Stopwatch stopwatch;
    private TableLayout table_laps;

    public LapsFragments(Stopwatch stopwatch) {
        this.stopwatch = stopwatch;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_laps, container, false);

        table_laps = (TableLayout) view.findViewById(R.id.table_laps);
        return view;
    }

    public void clear_laps() {
        table_laps.removeAllViews();
    }

    public void add_lap() {
        Long[] lap = stopwatch.addLap();

        TextView lap_info = new TextView(this.getActivity());
        lap_info.setText("Lap " + lap[2]);
        lap_info.setTextSize(25);
        lap_info.setPadding(0, 0, 5, 0);

        TextView lap_time = new TextView(this.getActivity());
        lap_time.setText("duration: " + stopwatch.convertToText(lap[1])
                + "\n" + "time: " + stopwatch.convertToText(lap[0]));

        table_laps.addView(lap_info, 0);
        table_laps.addView(lap_time, 1);
    }
}
